#!/bin/python3
# -*- coding:utf-8 -*-
"""
    Flask APP
    Add By :陈狍子 e4ting@qq.com 2024-07-19 16:50:34
"""
import sys,os
from pdb import set_trace as strace
from traceback  import format_exc as dumpstack
# from e4ting import util,log

from flask import Flask,request,make_response
from werkzeug.middleware.proxy_fix import ProxyFix
from flask_session  import Session
from flask_cors     import CORS

from config import FlaskConf

webapp = Flask(__name__, template_folder='/')

webapp.wsgi_app = ProxyFix(webapp.wsgi_app, x_proto=1)

webapp.config['SECRET_KEY'] = FlaskConf.SECRET_KEY
webapp.config['SQLALCHEMY_DATABASE_URI'] = FlaskConf.FLASK_DB_URI
# webapp.config['SQLALCHEMY_ECHO'] = True
webapp.config['SQLALCHEMY_POOL_SIZE'] = 5
webapp.config['SQLALCHEMY_POOL_TIMEOUT'] = 5
webapp.config["SQLALCHEMY_MAX_OVERFLOW"] = 0
webapp.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
webapp.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True

def ApiErr(msg="", code=500):
    return dict(msg=msg, code=code, data=None)

def ApiOK(data={}):
    return dict(msg="", code=200, data=data)

def app_context(*arg, **options):
    """
        采用上下文运行
    """
    import functools
    def _config(function):
        @functools.wraps(function)
        def wrapper(*args, **kwargs):
            with webapp.app_context():
                return function(*args, **kwargs)
        for k,v in options.items():
            setattr(wrapper, k, v)
        return wrapper
    return _config

import builtins
__builtins__["ApiErr"] = ApiErr
__builtins__["ApiOK"]  = ApiOK

